#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk, utils, os
import GMM_timeline.timeline as timeline
import gnomevfs
import config
conf=config.config()
import edit_frame, edit_text

from GMM_timeline import frame, track, text

import progress_window
import plugins
plugins.get_child_plugins()

#gtk.gdk.threads_init()

class main_window(gtk.Window):
    def __init__(self, project_folder):
        gtk.Window.__init__(self)
        self.project_folder=project_folder+"/"
        self.VBox=gtk.VBox()
        HBox=gtk.HBox()
        
        self.workspace=gtk.VBox()
        
        self.Timeline=timeline.timeline(self.project_folder, self.action)
        self.VBox.pack_end(self.Timeline, False)
        
        VButtonBox=gtk.VBox()
        VButtonBox.set_spacing(5)
        #VButtonBox.set_layout(gtk.BUTTONBOX_START)
        
        addPButton=gtk.Button("Add photo")
        addPButton.connect("clicked", self.add_picture)
        VButtonBox.pack_start(addPButton, False)

        addBPButton=gtk.Button("Add black photo")
        addBPButton.connect("clicked", self.add_black_picture)
        VButtonBox.pack_start(addBPButton, False)

        addFButton=gtk.Button("Add folder of photos")
        addFButton.connect("clicked", self.add_folder)
        VButtonBox.pack_start(addFButton, False)
        
        addTxtButton=gtk.Button("Add text")
        addTxtButton.connect("clicked", self.add_txt)
        VButtonBox.pack_start(addTxtButton, False)

        

        rmallButton=gtk.Button("Remove all frames")
        rmallButton.connect("clicked", self.clean_list)
        VButtonBox.pack_end(rmallButton, False)
        
        makeButton=gtk.Button("Make video")
        makeButton.connect("clicked", self.make_video)
        VButtonBox.pack_end(makeButton, False)
        
        HBox.pack_start(VButtonBox, False)
        HBox.pack_end(self.workspace)
        self.VBox.pack_start(HBox)
        self.add(self.VBox)
        self.set_size_request(780, 580)
        self.set_title("GMovieMaker")
        self.set_icon_from_file(conf.picture_path+"logo.png")
        self.show_all()
        self.connect("destroy-event", self.close)
        self.connect("delete-event", self.close)
        gtk.gdk.threads_init()
        gtk.main()
    
    def close(self, obj=None, obj2=None):
        self.hide()
        gtk.main_quit()
    
    def add_folder(self, obj=None):
    	folder=utils.GetFolder("Select input folder").get_folder()
    	if folder!=None:
    		folder+="/"
    		print folder
    		listafile=os.listdir(folder)
    		listafile.sort()
    		for photo in listafile:
    			#print folder+photo
    			if os.path.isfile(folder+photo) and photo.find(".")>0 and (gnomevfs.get_mime_type(folder+photo).split("/")[0]=="image"):
    				self.Timeline.add_frame(folder+photo)
    
    def add_picture(self, obj=None):
        photo=utils.OpenImage().get_file()
        if photo!=None:
            self.Timeline.add_frame(photo)
    
    def add_black_picture(self, obj=None):
        try:
            os.mkdir(self.projectfolder + "photos/")
        except: pass
        photo=self.project_folder+"photos/empty.png"
        if not (os.path.isfile(photo)):
            utils.execute(["convert" ,"-size", "800x600", "xc:transparent", "-fill", "black", "-draw", "rectangle 0,0,800,600", photo])
        self.Timeline.add_frame(photo)
    
    def add_txt(self, obj=None):
        self.Timeline.add_txt()
        pass
    
    def remove_picture(self, obj=None):
        #self.Timeline.remove()
        pass
    
    def clean_list(self, obj=None):
    	self.Timeline.remove_all_frames()
        pass
    
    def make_video(self, obj=None):
    	
        #import make_video
        self.hide()
        #gtk.main_quit()
        progress=progress_window.progress_window("Making the video", self.project_folder, self.Timeline.get_frame_list(), self.Timeline.get_text_list(), self.Timeline.get_track_list())
        
        #print "making the video"
        #make_vids=make_video.make_video(self.project_folder, self.Timeline.get_frame_list(), progress)
        #make_vids.start()
        #progress.hide()
        self.show_all()
    
    def action(self, obj):
        for widget in self.workspace.get_children(): self.workspace.remove(widget)
        
        if isinstance(obj, frame.frame):
            self.workspace.add(edit_frame.edit_frame(obj, self.Timeline.save, self.Timeline.remove_selected, self.Timeline.as_this))
            self.workspace.show_all()
        elif isinstance(obj, track.track):
            pass
        elif isinstance(obj, text.text):
            self.workspace.add(edit_text.edit_text(obj, self.Timeline.save, self.Timeline.remove_selected))
            self.workspace.show_all()